package flex.cc.single.api;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import flex.cc.PublicUtil;
import flex.cc.basicinformation.dao.ValueAddedServiceProviderDao;
import flex.cc.basicinformation.entity.ValueAddedServiceProviderEntity;
import flex.cc.common.utils.DateUtil;
import flex.cc.core.support.BaseController;
import flex.cc.drive.util.DriveDESUtil;
import flex.cc.single.service.DxVerifyService;
import flex.cc.util.APIConfigUtil;

/**
 * @author R
 * @desc 单项到店类核销通知接口
 */
@RestController
@RequestMapping(value = "/storeapi")
public class SingleVerifyController extends BaseController {

	@Resource
	private DxVerifyService dxVerifyService;
	@Autowired
    private ValueAddedServiceProviderDao valueAddedserviceProviderDao;
    @Autowired
    private APIConfigUtil apiConfigUtil;
    
	/**
	 * @author R
	 * @desc 单项到店类核销接口
	 */
	@ResponseBody
    @RequestMapping("/singleVerifyNotice")
    public JSONObject verifyNotice(HttpServletRequest request) throws Exception{
		JSONObject jsonResult = new JSONObject();
		String resultCode = "SUCCESS";
		String resultDesc = "成功";
		long startTime = System.currentTimeMillis();
    	logger.info("SingleVerifyController 进入单项核销接口:time=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
		try {
			request.setCharacterEncoding("UTF-8");// 设置编码
            // 读取请求报文
            String requestJson = IOUtils.toString(request.getInputStream(), "UTF-8");
            logger.info("SingleVerifyController 单项核销通知接口:reqJson="+requestJson);
            if (requestJson != null && !"".equals(requestJson)) {
            	JSONObject jsonObject = JSONObject.parseObject(requestJson);
            	Map<String, String> postMap = jsonObject.toJavaObject(Map.class);
            	String encryptJsonStr =  postMap.get("encryptJsonStr");
                String apiCode =  postMap.get("apiCode");//约束之后新服务商要传的唯一识别码
            	
            	if(PublicUtil.isNotEmpty(encryptJsonStr)){//加密字符串
            		String provider = "";
            		Map<String, Object> params = new HashMap<String, Object>();
                	if(PublicUtil.isEmpty(apiCode)){//如果为空默认盛大
                		params.put("apiCode", "SD");
                	}else{
                		params.put("apiCode", apiCode);
                	}
            		ValueAddedServiceProviderEntity serviceProviderEntity = valueAddedserviceProviderDao.getOneByParams(params);
                	if(PublicUtil.isNotEmpty(serviceProviderEntity)){
            			provider = String.valueOf(serviceProviderEntity.getId());
            		}else{
            			resultCode = "01";
            			resultDesc = "apiCode信息未配置，请联系业务人员";
                    	logger.error("SingleVerifyController 单项核销接口失败:apiCode信息未配置，请联系业务人员");
            		}
                    
                    String key = apiConfigUtil.getApiConfig(provider, "drive_decode");
                    String key3Des = apiConfigUtil.getApiConfig(provider, "store_key3Des");
                    
                	String verifyRequestJson = DriveDESUtil.driverDecrypt(encryptJsonStr,key,key3Des);
                	logger.info("SingleVerifyController 单项核销接口接收解密报文 "+verifyRequestJson);
                    Map<String, String> resultMap = dxVerifyService.verifyNotice(verifyRequestJson); 
                    String code = resultMap.get("code");
        			if("01".equals(code)){
        				resultCode = "01";
        				resultDesc = resultMap.get("msg");
            		}
            	}else{
            		resultCode = "01";
                	resultDesc = "接口处理异常:请求报文为空";
                	logger.error("SingleVerifyController 单项核销通知接口---处理异常:请求报文为空");
            	}
            }else {
            	resultCode = "01";
            	resultDesc = "接口处理异常:请求报文为空";
            	logger.error("SingleVerifyController 单项核销通知接口---处理异常:请求报文为空");
            }
    	} catch (Exception e) {
			resultCode = "ERROR";
			resultDesc = "数据处理异常";
        	logger.error("SingleVerifyController 单项核销确认接口异常::" + e.getMessage());
		}
		jsonResult.put("resultCode", resultCode);
		jsonResult.put("resultDesc", resultDesc);
		long endTime = System.currentTimeMillis();
        logger.info("SingleVerifyController 单项核销确认接口:endtime=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + ";澶勭悊鏃堕暱:" + (endTime - startTime));
		return jsonResult;
	}
	
	
}
